package com.rlz.base.weiget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.rlz.base.R;
import com.rlz.drawable.helper.Shape;

import androidx.annotation.IntDef;

/**
 * Created by RLZ
 * on 2021/6/8
 */
public class StateView extends FrameLayout {

    public final static int SUCCESS = 0;
    public final static int EMPTY = 1;
    public final static int ERROR = 2;
    public final static int NO_NETWORK = 3;

    private final Context mContext;
    private View mErrorPage;
    private View mEmptyPage;
    private View mNetworkPage;

    private TextView mErrorText;
    private TextView mEmptyText;
    private TextView mNetworkText;

    private View mContentView;
    private OnReloadListener mListener;
    private boolean isFirstVisible = false; //是否一开始显示contentView

    public StateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StateView);
        isFirstVisible = a.getBoolean(R.styleable.StateView_isFirstVisible, true);
        a.recycle();
    }

    public StateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
    }

    public StateView(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (getChildCount() > 1) {
            throw new IllegalStateException("StateView 只能有一个孩子");
        }
        mContentView = this.getChildAt(0);
        if (!isFirstVisible) {
            mContentView.setVisibility(View.GONE);
        }
        build();
    }

    @SuppressLint("InflateParams")
    private void build() {
        mErrorPage = LayoutInflater.from(mContext).inflate(R.layout.widget_error_page, null);
        mEmptyPage = LayoutInflater.from(mContext).inflate(R.layout.widget_empty_page, null);
        mNetworkPage = LayoutInflater.from(mContext).inflate(R.layout.widget_nonetwork_page, null);

        mErrorPage.setBackgroundColor(getColor(R.color.white));
        mEmptyPage.setBackgroundColor(getColor(R.color.white));
        mNetworkPage.setBackgroundColor(getColor(R.color.white));

        mErrorText = findViewById(mErrorPage, R.id.error_text);
        mEmptyText = findViewById(mEmptyPage, R.id.empty_text);
        mNetworkText = findViewById(mNetworkPage, R.id.no_network_text);

        ImageView errorImg = findViewById(mErrorPage, R.id.error_img);
        ImageView emptyImg = findViewById(mEmptyPage, R.id.empty_img);
        ImageView networkImg = findViewById(mNetworkPage, R.id.no_network_img);

        TextView errorReloadBtn = findViewById(mErrorPage, R.id.error_reload_btn);
        TextView networkReloadBtn = findViewById(mNetworkPage, R.id.no_network_reload_btn);

        errorReloadBtn.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onReload(v);
            }
        });
        networkReloadBtn.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onReload(v);
            }
        });

        mErrorText.setText(getContext().getString(R.string.sv_not_network));
        mEmptyText.setText(getContext().getString(R.string.sv_empty));
        mNetworkText.setText(getContext().getString(R.string.sv_not_network));

        errorImg.setImageResource(R.mipmap.icon_no_network);
        emptyImg.setImageResource(R.mipmap.icon_empty_common);
        networkImg.setImageResource(R.mipmap.icon_no_network);


        errorReloadBtn.setBackground(Shape.stroke(20f, R.color.global_text_color, 2));
        networkReloadBtn.setBackground(Shape.stroke(20f, R.color.global_text_color, 2));

        errorReloadBtn.setText(getContext().getString(R.string.sv_retry));
        networkReloadBtn.setText(getContext().getString(R.string.sv_retry));

        errorReloadBtn.setTextSize(14);
        networkReloadBtn.setTextSize(14);

        this.addView(mNetworkPage);
        this.addView(mEmptyPage);
        this.addView(mErrorPage);

        setStatus(SUCCESS);
    }

    public void setStatus(@Flavour int status) {

        switch (status) {
            case SUCCESS:
                setStatus(status, null);
                break;
            case EMPTY:
                setStatus(status, getContext().getString(R.string.sv_empty));
                break;
            case ERROR:
                setStatus(status, getContext().getString(R.string.sv_not_network));
                break;
            case NO_NETWORK:
                setStatus(status, getContext().getString(R.string.sv_not_network));
                break;
        }
    }

    public void setStatus(@Flavour int status, String msg) {

        switch (status) {
            case SUCCESS:

                mContentView.setVisibility(View.VISIBLE);
                mEmptyPage.setVisibility(View.GONE);
                mErrorPage.setVisibility(View.GONE);
                mNetworkPage.setVisibility(View.GONE);
                break;

            case EMPTY:

                mContentView.setVisibility(View.GONE);
                mEmptyPage.setVisibility(View.VISIBLE);
                mErrorPage.setVisibility(View.GONE);
                mNetworkPage.setVisibility(View.GONE);
                setEmptyText(msg);
                break;

            case ERROR:

                mContentView.setVisibility(View.GONE);
                mEmptyPage.setVisibility(View.GONE);
                mErrorPage.setVisibility(View.VISIBLE);
                mNetworkPage.setVisibility(View.GONE);
                setErrorText(msg);
                break;

            case NO_NETWORK:

                mContentView.setVisibility(View.GONE);
                mEmptyPage.setVisibility(View.GONE);
                mErrorPage.setVisibility(View.GONE);
                mNetworkPage.setVisibility(View.VISIBLE);
                setNoNetworkText(msg);
                break;

            default:
                break;
        }
    }


    /**
     * 设置Empty状态提示文本，仅对当前所在的地方有效
     */
    public void setEmptyText(String text) {
        mEmptyText.setText(text);
    }

    /**
     * 设置Error状态提示文本，仅对当前所在的地方有效
     */
    public void setErrorText(String text) {
        mErrorText.setText(text);
    }

    /**
     * 设置No_Network状态提示文本，仅对当前所在的地方有效
     */
    public void setNoNetworkText(String text) {
        mNetworkText.setText(text);
    }

    /**
     * 设置ReloadButton的监听器
     */
    public void setOnReloadListener(OnReloadListener listener) {
        this.mListener = listener;
    }


    @IntDef({SUCCESS, EMPTY, ERROR, NO_NETWORK})
    public @interface Flavour {

    }

    public interface OnReloadListener {
        void onReload(View v);
    }


    private  <T extends View> T findViewById(View v, int id) {
        return v.findViewById(id);
    }

    private int getColor(int colorId) {
        int color;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            color = getContext().getColor(colorId);
        } else {
            color = getContext().getResources().getColor(colorId);
        }
        return color;
    }
}